import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import { jwtGuard } from 'src/auth/guard';
import { PrismaService } from 'src/prisma/prisma.service';

@Controller('book')
export class BookController {
  constructor(private prisma: PrismaService) {}
  @Get('/')
  book(@Query() params) {
    return this.prisma.book.findMany({
      where: {
        TXM: params.TXM ? { contains: params.TXM } : undefined,
        bookId: params.bookId ? parseInt(params.bookId) : undefined,
        title: params.title ? { contains: params.title } : undefined,
        ISBN: params.ISBN ? { contains: params.ISBN } : undefined,
        author:params.author ? { contains: params.author } : undefined,
        TSLX:params.TSLX ? { contains: params.TSLX } : undefined,
        CBS:params.CBS ? { contains: params.CBS } : undefined,
        SJMC:params.SJMC ? { contains: params.SJMC } : undefined,
      },
    });
  }

  @UseGuards(jwtGuard)
  @Post('/')
  addBook(@Body() data) {
    console.log(data);
    return this.prisma.book.create({
      data: { ...data },
    });
  }

  @UseGuards(jwtGuard)
  @Put('/:id')
  changeBook(@Body() data, @Param() param) {
    console.log(data);
    return this.prisma.book.update({
      where: {
        bookId: parseInt(param.id),
      },
      data: { ...data },
    });
  }

  @UseGuards(jwtGuard)
  @Delete('/:id')
  deleteBook(@Param() param) {
    console.log(param);
    return [];
  }
}
